package com.lzx.musiclib.viewmodel

import android.content.Context
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.lzx.musiclib.TestApplication
import com.lzx.musiclib.forEach
import com.lzx.musiclib.getArray
import com.lzx.musiclib.utils.MusicUtil
import com.lzx.starrysky.SongInfo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.json.JSONObject
import java.io.BufferedInputStream

class MusicViewModel : ViewModel() {

    companion object {
        val baseUrl = "http://espoir.aifield.biz/"
    }

    fun getHomeMusic(context: Context) = getMusicList(context)


    private fun getMusicList(context: Context): MutableList<SongInfo> {

        val hubUrl = "github.com"

        //    https://raw.fastgit.org/EspoirX/lzxTreasureBox/master/e.aac
        val gg = "https://96.f.1ting.com/local_to_cube_202004121813/96kmp3/2020/04/28/28b_Yin/01.mp3"
        val a1 ="https://96.f.1ting.com/local_to_cube_202004121813/96kmp3/2020/01/03/03a_Panda/01.mp3"
        val a2 = "https://96.f.1ting.com/local_to_cube_202004121813/96kmp3/2019/07/26X/26h_Liuli/01.mp3"
        val life = "https://sharefs.ali.kugou.com/202110121305/7837e8d4415b357cf554d97c12535946/G111/M01/02/09/T5QEAFnkKKuAGa2HAEy82n4r_90335.mp3"
        val force = "https://other-web-nf03-sycdn.kuwo.cn/afe48a997f6b1b53aad2fac90262b717/6164faf1/resource/a3/61/73/282337866.aac"
//        val z =
//            "https://$hubUrl/EspoirX/lzxTreasureBox/raw/master/%E5%91%A8%E6%9D%B0%E4%BC%A6-%E5%91%8A%E7%99%BD%E6%B0%94%E7%90%83.mp3"
//        val a = "https://$hubUrl/EspoirX/lzxTreasureBox/raw/master/a.aac"
//        val b = "https://$hubUrl/EspoirX/lzxTreasureBox/raw/master/b.aac"
//        val c = "https://$hubUrl/EspoirX/lzxTreasureBox/raw/master/c.aac"
//        val d = "https://$hubUrl/EspoirX/lzxTreasureBox/raw/master/d.aac"
//        val e = "https://$hubUrl/EspoirX/lzxTreasureBox/raw/master/e.aac"
//        val f = "https://$hubUrl/EspoirX/lzxTreasureBox/raw/master/f.aac"
//        val g = "https://$hubUrl/EspoirX/lzxTreasureBox/raw/master/g.aac"

        val songList = mutableListOf<SongInfo>()
        songList.add(SongInfo("gg", gg, "gg"))
        songList.add(SongInfo("a1", a1, "踏山河"))
        songList.add(SongInfo("a2", a2, "a2"))
        songList.add(SongInfo("life", life, "life"))
        songList.add(SongInfo("force", force, "force"))
//        songList.add(SongInfo("z", z, "z"))
//        songList.add(SongInfo("a", a, "a"))
//        songList.add(SongInfo("b", b, "b"))
//        songList.add(SongInfo("c", c, "c"))
//        songList.add(SongInfo("d", d, "d"))
//        songList.add(SongInfo("e", e, "e"))
//        songList.add(SongInfo("f", f, "f"))
//        songList.add(SongInfo("g", g, "g"))

//        var json: String
//        val asset = TestApplication.context?.assets ?: return mutableListOf()
//        asset.open(file).use { it ->
//            BufferedInputStream(it).use {
//                json = it.reader().readText()
//            }
//        }
//        val arrayJson = JSONObject(json).getJSONObject("data")
//            .getJSONArray("songlist")
//
//        arrayJson.forEach<JSONObject> {
//            val info = SongInfo()
//            info.tag = tag
//            info.songName = it?.getString("songname").orEmpty()
        //            info.songId = it?.getString("songmid").orEmpty()
//            info.artist = it?.getArray("singer")?.getJSONObject(0)?.getString("name").orEmpty()
//            val albumid = it?.getString("albummid").orEmpty()
//            info.songCover = "https://y.gtimg.cn/music/photo_new/T002R300x300M000${albumid}.jpg"
//            list.add(info)
//        }
        val localMusic = getLocalMusic(context)
        songList.addAll(localMusic)
        return songList
    }


    private fun getLocalMusic(context: Context): MutableList<SongInfo> {
        val mp3InfoList = MusicUtil().getMp3InfoList(context)
        return mp3InfoList


    }

    var cardLiveData = MutableLiveData<MutableList<SongInfo>>()
    fun getCardMusicList(type: String?) {
        if (type.isNullOrEmpty()) return
        viewModelScope.launch(Dispatchers.IO) {
            var json: String?
            val asset = TestApplication.context?.assets
            asset?.open("card.json").use { it ->
                BufferedInputStream(it).use {
                    json = it.reader().readText()
                }
            }
            if (json.isNullOrEmpty()) {
                cardLiveData.postValue(mutableListOf())
            } else {
                val arrayJson = JSONObject(json).getJSONObject(type)
                    .getJSONArray("songlist")
                val list = mutableListOf<SongInfo>()
                arrayJson.forEach<JSONObject> {
                    val info = SongInfo()
                    info.songName = it?.getString("songname").orEmpty()
                    info.songId = it?.getString("songmid").orEmpty()
                    info.artist = it?.getArray("singer")?.getJSONObject(0)?.getString("name").orEmpty()
                    val albumid = it?.getString("albummid").orEmpty()
                    info.songCover = "https://y.gtimg.cn/music/photo_new/T002R300x300M000${albumid}.jpg"
                    list.add(info)
                }
                cardLiveData.postValue(list)
            }
        }
    }

    var dynamicLiveData = MutableLiveData<MutableList<SongInfo>>()
    fun getDynamicMusicList(typeText: String?) {
        var type = ""
        if (typeText == "推荐") {
            type = "recom"
        } else if (typeText == "最新") {
            type = "news"
        }
        if (type.isEmpty()) return
        viewModelScope.launch(Dispatchers.IO) {
            var json: String?
            val asset = TestApplication.context?.assets
            asset?.open("dynamic.json").use { it ->
                BufferedInputStream(it).use {
                    json = it.reader().readText()
                }
            }
            if (json.isNullOrEmpty()) {
                dynamicLiveData.postValue(mutableListOf())
            } else {
                val arrayJson = JSONObject(json).getJSONObject(type)
                    .getJSONArray("songlist")
                val list = mutableListOf<SongInfo>()
                arrayJson.forEach<JSONObject> {
                    val info = SongInfo()
                    info.songName = it?.getString("songname").orEmpty()
                    info.songId = it?.getString("songmid").orEmpty()
                    info.artist = it?.getArray("singer")?.getJSONObject(0)?.getString("name").orEmpty()
                    val albumid = it?.getString("albummid").orEmpty()
                    info.songCover = "https://y.gtimg.cn/music/photo_new/T002R300x300M000${albumid}.jpg"
                    list.add(info)
                }
                dynamicLiveData.postValue(list)
            }
        }
    }

    fun getUserMusicList(context: Context) = getMusicList(context)
}